$(document).ready(function() {
	var commonUrl = $(".commonUrl").text();
	//表单验证
	$("#addForm").validate({
		onfocusout: function(element) {
			$(element).valid();
		},
		rules: {
			clothName: "required",
			NO: "required",
			orderqty: "required",
			goodsquantity: "required",
			price: "required",
			mainapplication: "required",
			tcxcolorsystem: "required",
			tpxcolorsystem: "required",
		},
		messages: {
			clothName: "请输入面料名称",
			NO: "请输入货号",
			orderqty: "请输入订货起订量",
			goodsquantity: "请输入现货量",
			price: "请输入价格",
			mainapplication: "请输入主要用途",
			tcxcolorsystem: "请输入TCX色系",
			tpxcolorsystem: "请输入TPX色系",
		}
	});
	
	$(".imghead").each(function(index) {
		$(this).click(function() {
			$(".tohide")[index].click();

		});
	});
	//图片上传预览    IE是用了滤镜。
	$("input[class='file']")
			.each(
					function(index) {
						function previewImage(file) {
							var MAXWIDTH = 260;
							var MAXHEIGHT = 180;
							var div = $('.preview')[index];
							if (file.files
									&& file.files[index]) {
								div.innerHTML = '<img class=imghead>';
								var img = $('.imghead')[index];
								img.onload = function() {
									var rect = clacImgZoomParam(
											MAXWIDTH,
											MAXHEIGHT,
											img.offsetWidth,
											img.offsetHeight);
									img.width = rect.width;
									img.height = rect.height;
									img.style.marginTop = rect.top
											+ 'px';
								}
								var reader = new FileReader();
								reader.onload = function(
										evt) {
									img.src = evt.target.result;

								}
								reader
										.readAsDataURL(file.files[index]);
							} else //兼容IE
							{
								var sFilter = 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale,src="';
								file.select();
								var src = document.selection
										.createRange().text;
								div.innerHTML = '<img class=imghead>';
								var img = $('.imghead')[index];
								img.filters
										.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src;
								var rect = clacImgZoomParam(
										MAXWIDTH,
										MAXHEIGHT,
										img.offsetWidth,
										img.offsetHeight);
								status = ('rect:'
										+ rect.top + ','
										+ rect.left + ','
										+ rect.width + ',' + rect.height);
								div.innerHTML = "<div class=divhead style='width:" + rect.width + "px;height:" + rect.height + "px;margin-top:" + rect.top + "px;" + sFilter + src + "\"'></div>";
							}
						}

						function clacImgZoomParam(maxWidth,
								maxHeight, width, height) {
							var param = {
								top : 0,
								left : 0,
								width : width,
								height : height
							};
							if (width > maxWidth
									|| height > maxHeight) {
								rateWidth = width
										/ maxWidth;
								rateHeight = height
										/ maxHeight;
								if (rateWidth > rateHeight) {
									param.width = maxWidth;
									param.height = Math
											.round(height
													/ rateWidth);
								} else {
									param.width = Math
											.round(width
													/ rateHeight);
									param.height = maxHeight;
								}
							}
							param.left = Math
									.round((maxWidth - param.width) / 2);
							param.top = Math
									.round((maxHeight - param.height) / 2);
							return param;
						}
					});
	//图片上传
	$("input[type='file']")
			.change(
					function() {
						var imgObjPreview = $(this).parent().find("img");
						console.log($(this).prop("files"));
						if ($(this).prop("files")
								&& $(this).prop("files")[0]) {
							imgObjPreview
									.attr("src",window.URL.createObjectURL($(this).prop("files")[0]));
							if ($(this).prev().find(
									".imghead").attr("src") != "") {
								$(this).prev().find(".select").addClass("changesel");
								$(this).prev().find(".select").text("重新"+ $(this).prev().find(".select").text());

							}
						} else {
							//IE下，使用滤镜
							docObj.select();
							var imgSrc = document.selection
									.createRange().text;
							var localImagId = document
									.getElementById("localImag");
							//必须设置初始大小
							localImagId.style.width = "150px";
							localImagId.style.height = "180px";
							//图片异常的捕捉，防止用户修改后缀来伪造图片
							try {
								localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
								localImagId.filters
										.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
							} catch (e) {
								alert("您上传的图片格式不正确，请重新选择!");
								return false;
							}
							imgObjPreview.style.display = 'none';
							document.selection.empty();
						}
						return true;
					});

	$(".addbtn").click(function() {
			$('.body-color-brand').fadeOut(100);
			$('.hide-body-brand').slideUp(200);
			var name = $(".name").val();
			var introduction = $(".introduction").val();
			$.ajax({
				url: 'getClothing_class.do?introduction=' + introduction + '&name=' + name,
				success: function(data) {
					$(".brand_id").empty();
					$.each(data, function(n, item) {
						var result = "";
						result += "<option" + " value=" + "'" + item.brand_id + "'" + ">" + item.name + "</option>";
						$(".brand_id").append(result);
					})
				}
			})
		})
	
	//成分小类
	$("#parent_component").change(function() {
		var parent_code = $(this).val();
		$.ajax({
			url: commonUrl+'/fabricComponent/get_FCByParent_code.do',
			data: 'parent_code=' + parent_code,
			success: function(data) {
				$("#child_component").empty();
				$("#child_component").append("<option value='-1'>请选择小类</option>");
				$.each(data, function(n, item) {
					var result = "";
					result += "<option" + " value=" + "'" + item.code + "'" + ">" + item.name + "</option>";
					$("#child_component").append(result);
				})
			}
		})
	});
	//织法小类
	$("#parent_class").change(function() {
		var parent_code = $(this).val();
		$.ajax({
			url:  commonUrl+'/weave/getWeaveByParent_code.do',
			data: 'parent_code=' + parent_code,
			success: function(data) {
				$("#child_class").empty();
				$("#child_class").append("<option value='-1'>请选择小类</option>");
				$.each(data, function(n, item) {
					var result = "";
					result += "<option" + " value=" + "'" + item.code + "'" + ">" + item.name + "</option>";
					$("#child_class").append(result);
				})
			}
		})
	});

})